package edu.gyc.test;

import java.util.concurrent.atomic.AtomicInteger;

class FirstCounter{
    private static Integer count = 0;

    synchronized public  static void increment() {
        count++;
    }

    public static Integer getCount() {
        return count;
    }
}

 class SafeCounter {

    private static AtomicInteger count = new AtomicInteger(0);

    public static void increment() {
        count.getAndIncrement();
    }

     public static AtomicInteger getCount() {
         return count;
     }
 }

public class HiCounter {
    public static void main(String[] args) throws InterruptedException {

        for (int i = 0; i < 100; i++) {
            Runnable runnable = new Runnable() {


                @Override
                public void run() {
                    /*
                  try{
                      Thread.sleep((long) (10000 * Math.random()));
                  }catch (InterruptedException e) {

                  }

                     */
                    FirstCounter.increment();
                   // SafeCounter.increment();
                    System.out.println(Thread.currentThread().getName()+": "+FirstCounter.getCount());
                }
            };
            new Thread(runnable).start();
        }


    }
}
